package net.flydy.ywxcx.wxcloudrun.common;

/**
 * @author flydy
 */
public class BaseException extends RuntimeException{

    public static final I18NManager i18n = I18NManager.getManager("i18n");

    private String code;

    private String message;

    private String otherDesc;

    public BaseException(String code, Throwable e) {
        this.code = code;
        this.message = i18n.getString(code);
        if (e != null) {
            this.initCause(e);
        }
    }

    public BaseException(String code, String msg, Throwable e) {
        this.code = code;
        if (msg == null) {
            this.message = i18n.getString(code);
        }
        if (e != null) {
            this.initCause(e);
        }
    }

    public BaseException(StatusEnum statusEnum, Throwable e) {
        this.code = statusEnum.getCode();
        this.message = statusEnum.getMessage();
        if (e != null) {
            this.initCause(e);
        }
    }

    public static void checkAndThrow(boolean good, String code, Throwable e) {
        if (!good) {
            throw new BaseException(code, e);
        }
    }

    public static void checkAndThrow(boolean good, StatusEnum code, Throwable e) {
        if (!good) {
            throw new BaseException(code.getCode(), code.getMessage(), e);
        }
    }

    public static void checkAndThrow(boolean good, StatusEnum code, Throwable e, String otherDesc) {
        if (!good) {
            BaseException ee = new BaseException(code, e);
            ee.setOtherDesc(otherDesc);
            throw ee;
        }
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getOtherDesc() {
        return otherDesc;
    }

    public void setOtherDesc(String otherDesc) {
        this.otherDesc = otherDesc;
    }
}
